<?php

namespace app\admin\controller\workteam\analysis;

use app\admin\model\AuthGroup;
use app\common\controller\Backend;
use fast\Tree;
use think\Db;


/**
 *员工分析
 */
class Admin extends Backend
{
    protected $model = null;
    protected $searchFields = 'id,username,nickname';
    protected $groupList=[];
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Admin');
        $this->assignconfig("admin", ['id' => $this->auth->id]);
        $this->getGroupData();

    }

    protected function getGroupData(){

        $department = get_addon_info('department');
        if ($department && $department['state'] == 1) {
            $groupdata = \app\admin\model\department\Admin::getAllDepartmentsTreeArray($this->auth->id, $this->auth->isSuperAdmin());
        } else {
            $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
            $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
            $this->groupList=$groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
            Tree::instance()->init($groupList);
            $groupdata = [];
            if ($this->auth->isSuperAdmin()) {
                $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
                foreach ($result as $k => $v) {
                    $groupdata[$v['id']] = $v['name'];
                }
            } else {
                $result = [];
                $groups = $this->auth->getGroups();
                foreach ($groups as $m => $n) {
                    $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
                    $temp = [];
                    foreach ($childlist as $k => $v) {
                        $temp[$v['id']] = $v['name'];
                    }
                    $result[__($n['name'])] = $temp;
                }
                $groupdata = $result;
            }
        }




        $this->view->assign('groupdata', $groupdata);
    }

    /**
     * 数据查看
     * @return string|\think\response\Json
     * @throws \think\Exception
     */
    public function index(){
        $erchart = [
            'hours' => $this->teamhours(false),
        ];
        if ($this->request->isAjax()){
            return $this->success('','',$erchart);
        }
        $this->view->assign([
            'erchart' => $erchart,
            'get_hours'=>array_sum($erchart['hours']['data'][__('获得工时')]),
            'plan_hours'=>array_sum($erchart['hours']['data'][__('安排工时')]),
            'todo_task'=>array_sum($erchart['hours']['data'][__('完成数量')]),
            'plan_task'=>array_sum($erchart['hours']['data'][__('安排数量')]),

        ]);
        return $this->view->fetch();
    }
    /**
     * 团队工时
     * @internal
     */
    private function teamhours($is_ajax = true){

        $startDate = $this->request->param('start_date', null);
        $endDate = $this->request->param('end_date', null);
        $owner_user_id = $this->request->param('owner_user_id', 0, 'intval');
        $group_id = $this->request->param('group_id',isset($this->groupList[0]['id'])?$this->groupList[0]['id']:0, 'intval');
        $owner_user_ids=array();
        if ($owner_user_id > 0) {
            $owner_user_ids[]=$owner_user_id;
        }elseif($group_id&&$group_id>0){

            $auth=new \addons\workteam\library\Auth();
            $owner_user_ids=$auth->getDepartmentAdminIds($this->auth,$group_id);
            if (!$owner_user_ids){
                $this->error("没有数据");
            }
        }

        $this->model = new  \app\admin\model\workteam\Task();
        $data=$this->model->getHoursEchart($startDate,$endDate, $owner_user_ids);
        return $data;
    }

    /**
     * 添加
     * @internal
     */
    public function add()
    {
        $this->error();
    }

    /**
     * 编辑
     * @internal
     */
    public function edit($ids = null)
    {
        $this->error();
    }

    /**
     * 删除
     * @internal
     */
    public function del($ids = "")
    {
        $this->error();
    }

    /**
     * 批量更新
     * @internal
     */
    public function multi($ids = "")
    {
        // 管理员禁止批量操作
        $this->error();
    }
}
